﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace StudentManagement
{
    public class ByteConverter : IValueConverter
    {
        //源转目标
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte.TryParse(value?.ToString(), out byte a);
            byte.TryParse(parameter?.ToString(), out byte b);
            return a == b;
        }

        //目标转源
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool.TryParse(value.ToString(), out bool v);
            if (v)
            {
                return parameter;
            }
            return DependencyProperty.UnsetValue;
        }
    }


    /// <summary>
    /// 自定义事件转换
    /// </summary>
    public class DateTimeConver : IValueConverter
    {
        //当值从绑定源传播给绑定目标时，调用方法Convert
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            //return DependencyProperty.UnsetValue;
            DateTime date = (DateTime)value;
            return date.ToString("yyyy-MM-dd");
        }
        //当值从绑定目标传播给绑定源时，调用此方法ConvertBack
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = value?.ToString();
            DateTime txtDate;
            if (DateTime.TryParse(str, out txtDate))
            {
                return txtDate;
            }
            //return DependencyProperty.UnsetValue;
            return null;
        }
    }
}
